import 'dart:async';

import 'package:star_author/deer/demo/demo_page.dart';
import 'package:star_author/deer/login/login_router.dart';
import 'package:star_author/deer/res/constant.dart';
import 'package:star_author/deer/routers/fluro_navigator.dart';
import 'package:star_author/deer/routers/routers.dart';
import 'package:star_author/deer/util/app_navigator.dart';
import 'package:star_author/deer/util/device_utils.dart';
import 'package:star_author/deer/util/image_utils.dart';
import 'package:star_author/deer/util/theme_utils.dart';
import 'package:star_author/deer/widgets/fractionally_aligned_sized_box.dart';
import 'package:star_author/deer/widgets/load_image.dart';
import 'package:star_author/utils/common_util/shared_preferences/sp_util.dart';
import 'package:flutter/material.dart';
import 'package:flutter_swiper_null_safety/flutter_swiper_null_safety.dart';
import 'package:quick_actions/quick_actions.dart';
import 'package:rxdart/rxdart.dart';

class SplashPage extends StatefulWidget {
  const SplashPage();

  @override
  _SplashPageState createState() => _SplashPageState();
}

class _SplashPageState extends State<SplashPage> {
  int _status = 0;
  final List<String> _guideList = ['app_start_1', 'app_start_2', 'app_start_3'];
  StreamSubscription<dynamic>? _subscription;

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance?.addPostFrameCallback((_) async {
      await Device.initDeviceInfo();
      if (SpUtil.getBool(Constant.keyGuide, defValue: true)!) {
        /// 预先缓存图片，避免直接使用时因为首次加载造成闪动
        void precacheImages(String image) {
          precacheImage(
              ImageUtils.getAssetImage(image, format: ImageFormat.webp),
              context);
        }

        _guideList.forEach(precacheImages);
      }
      _initSplash();
    });

    if (Device.isAndroid) {
      const QuickActions quickActions = QuickActions();
      quickActions.initialize((String shortcutType) async {
        if (shortcutType == 'demo') {
          AppNavigator.pushReplacement(context, const DemoPage());
          _subscription?.cancel();
        }
      });
    }
  }

  @override
  void dispose() {
    _subscription?.cancel();
    super.dispose();
  }

  void _initGuide() {
    setState(() {
      _status = 1;
    });
  }

  void _initSplash() {
    _subscription =
        Stream.value(1).delay(const Duration(milliseconds: 1500)).listen((_) {
      if (SpUtil.getBool(Constant.keyGuide, defValue: true)! ||
          Constant.isDriverTest) {
        SpUtil.putBool(Constant.keyGuide, false);
        _initGuide();
      } else {
        _goLogin();
      }
    });
  }

  void _goLogin() {
    String phone = SpUtil.getString(Constant.phone) ?? "";
    if (phone.isNotEmpty) {
      NavigatorUtils.push(context, Routes.home, replace: true);
    } else {
      NavigatorUtils.push(context, LoginRouter.loginPage, replace: true);
    }
  }

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
        child: Material(
            color: context.backgroundColor,
            child: _status == 0
                ? const FractionallyAlignedSizedBox(
                    heightFactor: 0.3,
                    widthFactor: 0.33,
                    leftFactor: 0.33,
                    bottomFactor: 0,
                    child: LoadAssetImage('logo'))
                : Swiper(
                    key: const Key('swiper'),
                    itemCount: _guideList.length,
                    loop: false,
                    itemBuilder: (_, index) {
                      return LoadAssetImage(
                        _guideList[index],
                        key: Key(_guideList[index]),
                        fit: BoxFit.cover,
                        width: double.infinity,
                        height: double.infinity,
                        format: ImageFormat.webp,
                      );
                    },
                    onTap: (index) {
                      if (index == _guideList.length - 1) {
                        _goLogin();
                      }
                    },
                  )),
        onWillPop: () {
          return Future.value(false);
        });
  }
}
